/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.entity.element;

import com.inspur.edp.bef.bizentity.GspBizEntityElement;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bff.designtime.generator.VmGeneratorContext;
import com.inspur.edp.bff.designtime.generator.entity.VmEntityGenContext;
import com.inspur.edp.bff.designtime.generator.entity.node.VoNodeGeneratorContext;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.caf.generator.property.PropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspAssociation;
import com.inspur.edp.cef.generator.entityGenerator.base.DataTypeGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.AssoPropertyGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.ElementAssoGenerator;
import com.inspur.edp.das.commonmodel.IGspCommonElement;
import com.inspur.edp.formserver.viewmodel.GspViewModelElement;
import com.inspur.edp.lcm.metadata.api.service.RefCommonService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

import static io.iec.edp.caf.runtime.config.BeanUtil.getAppCtx;

public class VoAssoGenerator extends ElementAssoGenerator {
    private GspAssociation voAsso;
    private VoNodeGeneratorContext voNodeGeneratorContext;

    public VoAssoGenerator(GspAssociation asso, DataTypeGeneratorContext parentContext, VoNodeGeneratorContext voNodeGeneratorContext) {
        super(asso.getBelongElement(), asso, parentContext);
        voAsso = asso;
        this.voNodeGeneratorContext = voNodeGeneratorContext;
    }

    private RefCommonService lcmDtService;

    private RefCommonService getLcmDtService() {
        if (lcmDtService == null) {
            lcmDtService = SpringBeanUtils.getBean(RefCommonService.class);
        }
        return lcmDtService;
    }

    // region Properties
    @Override
    protected java.util.ArrayList<ClassPropertyGenerator> createPropertyGenerators() {
        GspViewModelElement vmElement = (GspViewModelElement) ((voAsso.getBelongElement() instanceof GspViewModelElement) ? voAsso.getBelongElement() : null);
        String beId = vmElement.getMapping().getTargetMetadataId();
        GspBusinessEntity refBe;
        //if (vmGenContext.RefEntityCollection.ContainsKey(beId))
        //    refBe = vmGenContext.RefEntityCollection[beId] as GspBusinessEntity;
        //else
        //refBe = (GspBusinessEntity) (getLcmDtService().getRefMetadata(beId).getContent());
        refBe=(GspBusinessEntity)voNodeGeneratorContext.getEntityGenContext().getCefGenContext().getMetadataJitContext().getMetadata(beId).getContent();
        //refBe = (GspBusinessEntity) (GenUtils.getJitcontext().getMetadata(beId).getContent());

        Object tempVar = refBe.findElementById(vmElement.getMapping().getTargetElementId());
        GspBizEntityElement entityElement = (GspBizEntityElement) ((tempVar instanceof GspBizEntityElement) ? tempVar : null);
        if (entityElement == null) {
            throw new RuntimeException("引用实体" + refBe.getCode() + "中，没有找到Id为" + vmElement.getMapping().getTargetElementId() + "的字段");
        }

        //Type beAssoType;
        //if (refBe.ID != vmGenContext.RefBizEntity.ID)
        //{
        //    string strbeEntityAssembly = refBe.GetEntityAssemblyInfo().AssemblyName;
        //    var refBeAssembly = Utils.GetAssembly(refBe.ID, strbeEntityAssembly, vmGenContext.BinPath, vmGenContext.VoGeneratePath);
        //    beAssoType = GenUtils.GetBEAssoType(refBeAssembly, entityElement);
        //}
        //else
        //{
        //    beAssoType = GenUtils.GetBEAssoType(vmGenContext, entityElement);
        //}

        GspAssociation beAsso = GetBeAssociation(entityElement);
//C# TO JAVA CONVERTER TODO TASK: There is no equivalent to implicit typing in Java:
//C# TO JAVA CONVERTER TODO TASK: Lambda expressions and anonymous methods are not converted by C# to Java Converter:
        ArrayList<ClassPropertyGenerator> rez = new ArrayList<ClassPropertyGenerator>();
        rez.add(new VoAssoPropertyGenerator((IGspCommonElement) (voAsso.getBelongElement()), voAsso, voNodeGeneratorContext));
        for (IGspCommonField element : voAsso.getRefElementCollection()) {
            rez.add(new VoAssoPropertyGenerator((IGspCommonElement) element, voAsso, voNodeGeneratorContext));
        }

        return rez;
    }

    private GspAssociation GetBeAssociation(GspBizEntityElement entityElement) {
        return (GspAssociation) ((entityElement.getChildAssociations().getItem(voAsso.getId())));
    }
}
